<?php

namespace app\lnk\controller\api;

use think\Request;
use app\appcenter\lib\AppIHookService;
use app\lnk\model\CircleModel;
use app\lnk\model\CircleFansModel;
use app\lnk\model\DynamicModel;
use app\lnk\model\TopicModel;
use app\lnk\model\UserModel;
use app\lnk\service\DynamicService;
use lite\lib\help;
use app\lnk\service\OperService;

class Circle
{






    public function get_circle_list(Request $request)
    {
        $data = OperService::_get_circle_list($request);
        return success('成功', $data);
    }

    public function get_circle_details(Request $request)
    {
        $id = $request->get('id');
        $uid = $request->uid;
        $data = CircleModel::where('status', 1)
            ->where('id', $id)
            ->field('id,avatar,name,intro,integral,type,update_time')
            ->find();
        if ($data) {
            $data['is_follow'] = CircleFansModel::where('user_id', $uid)
                ->where('circle_id', $data['id'])
                ->count() ? true : false;

            $data['user'] = [];
            $data['dynamic_count'] = DynamicModel::where('circle_id', $id)->where('status', 1)->count();
            $data['user_count'] = CircleFansModel::where('circle_id', $id)->count();
            if ($data['user_count']) {
                $uids = CircleFansModel::where('circle_id', $data['id'])
                    ->limit(3)
                    ->column('user_id');
                $data['user'] = UserModel::whereIn('id', $uids)->column('avatar');
            }
            $data['update_time_str'] = help::time_text($data['update_time']);
            return success('成功', $data);
        } else {
            return error('未找到圈子或圈子异常！', 401);
        }
    }



    //关注圈子
    public function follow_circle(Request $request)
    {
        $uid = $request->uid;
        $id = $request->post('id');
        $is_follow = $request->post('is_follow');
        if ($is_follow) {
            $weigh = CircleFansModel::where('user_id', $uid)->where('circle_id', $id)->value('weigh');
            if ($weigh == 999) return error('不可以退出自己创建的圈子');
            CircleFansModel::where('user_id', $uid)
                ->where('circle_id', $id)
                ->delete();
        } else {
            AppIHookService::Hook_Run('follow_circle',$data,['uid'=>$uid,'cid'=>$id]);
            CircleFansModel::insert([
                'user_id' => $uid,
                'circle_id' => $id,
                'create_time' => time()
            ]);
            AppIHookService::Hook_Run('follow_circle_out',$data,['uid'=>$uid,'cid'=>$id]);
        }
        
        return success('成功', '');
    }
    // 用户圈子
    public function user_circle(Request $request)
    {
        $data = OperService::_user_circle($request);
        return success('成功', $data);
    }

    public function circle_fans(Request $request)
    {

        $data = OperService::_circle_fans($request);
        return success('成功', $data);
    }

    public function dynamic_circle(Request $request)
    {
        $uid = $request->uid;
        $cid = $request->get('cid');

        $ids = CircleFansModel::where('user_id', $uid)->column('circle_id');
        if($cid>0) $ids = array_merge($ids, [$cid]);
        $ids = array_merge($ids, [2,3]);
        if (count($ids)) {
            $data = CircleModel::where('status', 1)
                ->whereIn('id', $ids)
                ->order('weigh', 'desc')
                ->field('id,name,avatar')
                ->select();
        }
        return success('成功', $data ?? []);
    }
    //活跃用户
    public function get_hot_user(Request $request)
    {
        $data = UserModel::where('status', 1)
        ->field('id,name,avatar')
        ->limit(15)
        ->order('end_time', 'desc')
        ->select()->toArray();
        return success('成功', $data);
    }

    //热点话题
    public function get_hot_topic(Request $request)
    {
        $data = TopicModel::where('status', 1)
        ->field('id,name,dynamic_count,view_count,highlight')
        ->limit(15)
        ->order('weigh', 'desc')
        ->select()->toArray();
        $colors = [
            '#9C27B0',
            '#e8850a',
            '#009688',
            '#2196F3',
            '#ea10bb',
            '#2522d0',
        ];
        foreach($data as &$v){
            shuffle($colors);
            $v['color'] = $colors[0];
        }
        return success('成功', $data);
    }


    public function get_top_circle(Request $request)
    {
        $data = CircleModel::where('status', 1)
            ->field('id,name,highlight,avatar,update_time')
            ->order('weigh', 'desc')
            ->order('create_time', 'desc')
            ->limit(5)
            ->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['user'] = [];
            $data[$k]['user_count'] = CircleFansModel::where('circle_id', $v['id'])->count();
            if ($data[$k]['user_count']) {
                $uids = (new CircleFansModel())
                    ->where('circle_id', $v['id'])
                    ->limit(3)
                    ->column('user_id');
                $data[$k]['user'] = UserModel::whereIn('id', $uids)->column('avatar');
            }
            $data[$k]['is_new'] = $data[$k]['user_count'] > 0;
        }
        //$data = array_chunk($data, 5);
        return success('成功', $data);
    }

    public function get_circle_dynamic(Request $request)
    {
        $uid = $request->uid;
        $id = $request->get('id');
        $type = $request->get('type', 0);
        $order = ['top' => 'desc', 'create_time' => 'desc', 'weigh' => 'desc', 'view' => 'desc'];
        if ($type == 1) $order = ['create_time' => 'desc', 'weigh' => 'desc', 'view' => 'desc'];
        if ($type == 2) $order = ['weigh' => 'desc', 'create_time' => 'desc', 'view' => 'desc'];
        $data = DynamicModel::where('status', 1)
            ->where('show', 1)
            ->where('circle_id', $id)
            ->order($order)
            ->field(DynamicService::dynamic_find())
            ->paginate(6)->toArray();
        $data = DynamicService::dynamic_map($data, true, $uid);

        return success('成功', $data);
    }

}
